package com.ybc.chinese.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.viewpagerindicator.CirclePageIndicator;
import com.ybc.chinese.R;
import com.ybc.chinese.activity.WebActivity;
import com.ybc.chinese.bean.CarouselFigure;
import com.ybc.chinese.utils.ImageUtil;
import com.ybc.chinese.widget.autoScroll.AutoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * <p><p/>
 * Created by Yubch on 17/2/18.
 */
public class AutoScrollPagerAdapter extends PagerAdapter {
    private Context mCtx;
    private AutoScrollViewPager mViewPager;
    private CirclePageIndicator indicator;
    private List<CarouselFigure> mDataList;

    public AutoScrollPagerAdapter(Context ctx, AutoScrollViewPager viewPager,
                                  CirclePageIndicator indicator) {
        this.mCtx = ctx;
        this.mViewPager = viewPager;
        this.indicator = indicator;
        mDataList = new ArrayList<>();
    }

    public void setDataList(List<CarouselFigure> dataList) {
        mDataList.clear();
        if (dataList != null) {
            mDataList.addAll(dataList);
        }
    }

    @Override
    public int getCount() { // 获得size
        return mDataList.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(View view, int position, Object object) // 销毁Item
    {
        if (object instanceof View) {
            ((ViewPager) view).removeView((View) object);
        }
    }

    @Override
    public Object instantiateItem(View view, final int position) // 实例化Item
    {
        final CarouselFigure carouselFigure = mDataList.get(position);
        final View v = LayoutInflater.from(mCtx).inflate(R.layout.item_auto_scroll, null);
        ImageView iv = (ImageView) v.findViewById(R.id.iv_item);
        ImageUtil.displayPic(mCtx, iv, carouselFigure.getPic().getUrl(), R.mipmap.ic_default);
        ((ViewPager) view).addView(v, 0);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                WebActivity.newIntent(mCtx, carouselFigure.getUrl(), carouselFigure.getTitle());
            }
        });
        return v;
    }

    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }

    @Override
    public final void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        // 若不止一条数据，则开启自动播放
        if (getCount() > 1) {
            try {
                indicator.setVisibility(View.VISIBLE);
                mViewPager.startAutoScroll();
            } catch (Exception ignore) {
                ignore.printStackTrace();
            }
        } else {
            try {
                indicator.setVisibility(View.GONE);
                mViewPager.stopAutoScroll();
            } catch (Exception ignore) {
                ignore.printStackTrace();
            }
        }
    }
}
